<?php

include "../include/ini.php";
switch (get('action')) {
    case "report":
        $data = explode("__", $_POST['uid']);
        $uid = array_shift($data);
        $date = array_shift($data);
        $month = date("Ym", strtotime($date));
        $order = false;
        if ($_POST['order'] == 1) {
            $order = "1";
        } elseif ($_POST['order'] == 0.4) {
            $order = "0.4";
        } elseif ($_POST['order'] == 0.5) {
            $order = "0.5";
        } else {
            $order = "0";
        }
        $insert = array("uid" => $uid, "date" => $date, "order" => $order, "month" => $month);
        print_r($insert);
        if (isset($order)) {
            mysql_insert_or_update('food', $insert, array("uid" => $uid, "date" => $date));
            set_totals($date);
        }
        break;
    case "load_menu":
        //echo post('date');
        $q = "SELECT `menu`, `disabled` FROM `date` WHERE `date` = '" . post('date') . "'";
        $menu = mysql_row_assoc($q);
        $disabled = $menu['disabled'];
        $menu = unserialize($menu['menu']);
        $div = "<div class='day_menu popup'>
            <input type=\"hidden\" id=\"date\" value=\"" . post('date') . "\" /> 
            <div class='menudate'>" . date("d.m.Y", strtotime(post('date'))) . "</div>
                <div class=\"clear\"></div>
                <div class='menuicon'><img src='" . BASE_URL . "source/img/soup.png' title='Первое' style='width: 94%; margin-left: 1%;' /></div>
                <div class='menucontent' title='Первое'><input class='menuinput' type=\"text\" id=\"menu_fc\" value=\"" . $menu['fc'] . "\" class=\"menuarea\" /></div>
                <div class=\"clear\"></div>
                <div class='menuicon'><img src='" . BASE_URL . "source/img/Steak-icon.png' title='Второе' style='width: 94%; margin-left: 1%;' /></div>
                <div class='menucontent' title='Второе'><input class='menuinput' type=\"text\" id=\"menu_mc\" value=\"" . $menu['mc'] . "\" class=\"menuarea\" /></div>
                <div class=\"clear\"></div>
                <div class=\"addmenu\">set</div>
                <div class='menuicon' title='Заблокировать' style='margin: 2px 4px 4px 2px; background-color: #FF554E;'><input type='checkbox' style='margin-top: 10px;' value='1' id='menu_disable' title='Заблокировать' " . ($disabled ? "checked='checked'" : "") . " /></div>
                </div>
";
        echo $div;
        break;
    case "show_menu":
//        echo post('date');
        $dateuid = explode("__", post('date_uid'));
        $date = array_pop($dateuid);
        $q = "SELECT `menu` FROM `date` WHERE `date` = '" . $date . "'";
        $menu = unserialize(mysql_value($q));
        $menu['fc'] = isset($menu['fc']) && $menu['fc'] ? $menu['fc'] : "&nbsp;";
        $menu['mc'] = isset($menu['mc']) && $menu['mc'] ? $menu['mc'] : "&nbsp;";
        $div = "<div class='popup order_active menu_" . $date . "' style='top: " . post('top') . "px; left: " . post('left') . "px'><input type=\"hidden\" id=\"date\" value=\"" . $date . "\" /> 
            <div class='menudate'>" . date("d.m.Y", strtotime($date)) . "</div>
                <div class=\"clear\"></div>
                    <div class='menuicon'><img src='" . BASE_URL . "source/img/soup.png' title='Первое' style='width: 94%; margin-left: 1%;' /></div>
                    <div class='menucontent' title='Первое'>" . $menu['fc'] . "</div>
                <div class=\"clear\"></div>
                    <div class='menuicon'><img src='" . BASE_URL . "source/img/Steak-icon.png' title='Второе' style='width: 94%; margin-left: 1%;' /></div>
                    <div class='menucontent' title='Второе'>" . $menu['mc'] . "</div>
                <div class=\"clear\"></div>
                    <div class=\"menubutton mb_left\" property='1'>Целый</div>
                    <div class=\"menubutton mb_right\" property='0.5'>Второе</div>
                <div class=\"clear\"></div>
                    <div class=\"menubutton mb_left\" property='0.4'>Первое</div>
                    <div class=\"menubutton mb_right\" property='0'>Не буду</div>
            </div>
";
//        $div = "<div class='order_active' style='top: 100px; left: 100px'><div property='0.4' class='fc button'></div><div property='0.5' class='mc button'></div><div property='1' class='all button'></div><div property='0' class='none button'></div></div>";
        echo $div;
        break;
    case "update_menu":
        $insert = array("date" => post('date'));
        $insert['menu'] = serialize(array("fc" => post("fc"), "mc" => post("mc")));
        $insert['disabled'] = post('disabled');
        $insert['month'] = date('Ym', strtotime($insert['date']));
        mysql_insert_or_update("date", $insert, array("date" => $insert['date']));
        set_totals($insert['date']);
        break;
    case "get_disabled":
        $month = mysql_value("SELECT `month` FROM `date` WHERE `date` = '" . post('date') . "'");
        $dates = mysql_select_array("SELECT `date` FROM `date` WHERE `month` = '" . $month . "' ORDER BY `date` ASC");
        $table = array();
        foreach ($dates as $key => $date) {
            if ($disabled = mysql_value("SELECT 1 FROM `date` WHERE `date` = '" . $date['date'] . "' AND `disabled` = 1")) {
                $table[$date['date']] = 1;
            } else {
                $order_options = array("" => "0", "0" => "0", "0.4" => "П", "0.5" => "В", "1" => "Ц");
                $total = mysql_value("SELECT `total` FROM `date` WHERE `date` = '" . $date['date'] . "' AND `disabled` = 0");
                $total = unserialize($total);
                $orders = mysql_select_array("SELECT `uid`, `order` FROM `food` WHERE `date` = '" . $date['date'] . "'");
                foreach ($orders as $key => $order) {
                    $orders[$order['uid'] . "__" . $date['date']] = $order_options[$order['order']];
                    $total[$order['uid'] . "__" . $date['date']] = $order_options[$order['order']];
                    unset($orders[$key]);
                }
                if ($orders) {
                    $total['keys'] = array_keys($orders);
                }
                $total = json_encode($total);
                if ($total) {
                    $table[$date['date']] = $total;
                }
            }
        }
        $table = json_encode($table);
        echo $table;

        break;
    case "exit":
        logout();
        break;
//    case "":
//        break;
//    case "":
//        break;
//    case "":
//        break;
//    case "":
//        break;
    default:
        break;
}